package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.Personnel;
import com.un.ebs.sys.domain.PersonnelExt;
import com.un.ebs.sys.dto.PersonnelAddDto;
import com.un.ebs.sys.dto.PersonnelRegisterDto;
import com.un.ebs.sys.dto.PersonnelUpdateDto;
import com.un.ebs.sys.service.PersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/personnel")
@Api(value = "sys-personnel", description = "人员")
public class PersonnelController {

    @Autowired
    private PersonnelService service;

    @PostMapping("register")
    @ApiOperation("人员注册")
    public WebResult register(@RequestBody @Validated PersonnelRegisterDto req) {
        service.register(req);
        return WebUtils.success();
    }

    @PostMapping("registerAudit")
    @ApiOperation("注册审核")
    public WebResult registerAudit(@RequestParam String id) {
        service.registerAudit(id);
        return WebUtils.success();
    }

    @PostMapping("moveToDept")
    @ApiOperation("移动")
    public WebResult moveToDept(@RequestParam String id, @RequestParam String deptId) {
        service.moveToDept(id, deptId);
        return WebUtils.success();
    }

    @PostMapping("invitePersonnel")
    @ApiOperation(value = "邀请", notes = "邀请人员到本企业")
    public WebResult invitePersonnel(@RequestParam String personnelId, @RequestParam String deptId) {
        service.invite(service.queryById(personnelId).getUserId(), deptId);
        return WebUtils.success();
    }

    @PostMapping("inviteUser")
    @ApiOperation(value = "邀请", notes = "邀请用户到本企业")
    public WebResult inviteUser(@RequestParam String userName, @RequestParam String deptId) {
        service.invite(userName, deptId);
        return WebUtils.success();
    }

    @PostMapping("add")
    @ApiOperation(value = "添加", notes = "添加")
    public WebResult save(@RequestBody PersonnelAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", notes = "修改")
    public WebResult update(@RequestBody PersonnelUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据主键获取单个 Personnel")
    public WebResult<Personnel> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询", notes = "可按姓名和电话号码查询人员信息，当为空时查所有人员。")
    public WebResult<IPage<PersonnelExt>> list(@RequestParam(required = false) String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.query(searchText, null, pageIndex, pageSize));
    }

    @GetMapping("list/{deptId}")
    @ApiOperation(value = "查询", notes = "可按姓名和电话号码查询人员信息，当为空时查所有人员。")
    public WebResult<IPage<PersonnelExt>> listByDept(@PathVariable("deptId") String deptId, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.query(null, deptId, pageIndex, pageSize));
    }

    @GetMapping("listByRoleCode")
    @ApiOperation(value = "查询", notes = "按角色代码")
    public WebResult<IPage<PersonnelExt>> listByRoleCode(@RequestParam String roleCode, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryByRoleCode(roleCode, pageSize, pageIndex));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("deleteToPersonnel")
    @ApiOperation(value = "删除", notes = "员工，并将员工的数据转移动到另一个员工名下")
    public WebResult deleteToPersonnel(@RequestParam String personnelId, @RequestParam String toPersonnelId) {
        return WebUtils.success(service.delete(personnelId, toPersonnelId));
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "排序", notes = "修改 Personnel 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

}